// pages/myMsg/myMsg.js
const api = require("../../api/api")
const app = getApp()
Page({
  data: {
    active: 0,
    show1: [true, true, true, true],
    showBuyList: [],
    iid: 0 //active的值
  },
  onChange(event) {
    console.log(event)
    this.setData({
      active: event.detail.name,
      showBuyList: [],
      iid: event.detail.name
    })
    switch (event.detail.name) {
      case 0:
        this.getGoodsZero()
        break;
      case 1:
        this.getGoodsOne()
        break;
      case 2:
        this.getGoodsTwo()
        break;
      default:
        this.getGoodsThree()
        break;
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      active: parseInt(options.active),
      iid: parseInt(options.active)
    })
    switch (parseInt(options.active)) {
      case 0:
        this.getGoodsZero()
        break;
      case 1:
        this.getGoodsOne()
        break;
      case 2:
        this.getGoodsTwo()
        break;
      default:
        this.getGoodsThree()
        break;
    }

  },

  getGoodsZero() {
    wx.request({
      url: api.getMyOrder.url,
      method: api.getMyOrder.method,
      data: {
        id: app.globalData.id,
        status: 0
      },
      success: res => {
        console.log(res.data.data)
        var list = res.data.data
        for (let index = 0; index < list.length; index++) {
          list[index]["img"] = list[index]["img"].split('|')
        }
        this.setData({
          showBuyList: list
        })
        //根据active中是否有数据判断是否显示vant组件（暂无订单）        
        if (this.data.showBuyList.length) {
          var showshow = this.data.show1
          showshow[this.data.active] = false
          this.setData({
            show1: showshow
          })
        } else {
          var showshow = this.data.show1
          showshow[this.data.active] = true
          this.setData({
            show1: showshow
          })
        }
        //-------------------------------------------------        
      },
    })
  },
  getGoodsOne() {
    wx.request({
      url: api.getMyOrder.url,
      method: api.getMyOrder.method,
      data: {
        id: app.globalData.id,
        status: 1
      },
      success: res => {
        console.log(res.data.data)
        var list = res.data.data
        for (let index = 0; index < list.length; index++) {
          list[index]["img"] = list[index]["img"].split('|')
        }
        this.setData({
          showBuyList: list,

        })
        //根据active中是否有数据判断是否显示vant组件（暂无订单）        
        if (this.data.showBuyList.length) {
          var showshow = this.data.show1
          showshow[this.data.active] = false
          this.setData({
            show1: showshow
          })
        } else {
          var showshow = this.data.show1
          showshow[this.data.active] = true
          this.setData({
            show1: showshow
          })
        }
        //-------------------------------------------------        
      },
    })
  },
  getGoodsTwo() {
    wx.request({
      url: api.getMyOrder.url,
      method: api.getMyOrder.method,
      data: {
        id: app.globalData.id,
        status: 2
      },
      success: res => {
        var list = res.data.data
        console.log(list)
        for (let index = 0; index < list.length; index++) {
          list[index]["img"] = list[index]["img"].split('|')
        }
        this.setData({
          showBuyList: list
        })
        //根据active中是否有数据判断是否显示vant组件（暂无订单）        
        if (this.data.showBuyList.length) {
          var showshow = this.data.show1
          showshow[this.data.active] = false
          this.setData({
            show1: showshow
          })
        } else {
          var showshow = this.data.show1
          showshow[this.data.active] = true
          this.setData({
            show1: showshow
          })
        }
        console.log(this.data.showBuyList)
        //-------------------------------------------------        
      },
    })
  },
  getGoodsThree() {
    wx.request({
      url: api.getMyGoods.url,
      method: api.getMyGoods.method,
      data: {
        userId: app.globalData.id
      },
      success: res => {
        console.log(res)
        var temp = this.data.showBuyList
        var list = res.data.data
        console.log(list.length)
        for (let index = 0; index < list.length; index++) {
          list[index]['img'] = list[index]['img'].split('|')
        }
        this.setData({
          showBuyList: list
        })
        //根据active中是否有数据判断是否显示（暂无订单）        
        if (this.data.showBuyList.length) {
          var showshow = this.data.show1
          showshow[this.data.active] = false
          this.setData({
            show1: showshow
          })
        } else {
          var showshow = this.data.show1
          showshow[this.data.active] = true
          this.setData({
            show1: showshow
          })
        }
      }
    })
  },
  onShow() {
    switch (this.data.active) {
      case 0:
        this.getGoodsZero()
        break;
      case 1:
        this.getGoodsOne()
        break;
      case 2:
        this.getGoodsTwo()
        break;
      default:
        // if (!this.data.showBuyList) {
        this.getGoodsThree()

        // }

        break;
    }
  },
  lisConOrder(e) {
    console.log(e)
    this.setData({
      active: e.detail
    })
    this.getGoodsTwo()
  }
})